<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Admin\Logic\OrderLogic;
use Think\AjaxPage;

class StatementController extends BaseController {

	/*
	 *订单首页
	 */
	public function offerList() {
		$begin = date('Y/m/d', (time() - 30 * 60 * 60 * 24));
		//30天前
		$end = date('Y/m/d', strtotime('+1 days'));
		$this -> assign('timegap', $begin . '-' . $end);
		$this -> display();
	}

	/*
	 *Ajax首页
	 */
	public function ajaxindex() {
		$orderLogic = new OrderLogic();
		$timegap = I('timegap');
		if ($timegap) {
			$gap = explode('-', $timegap);
			$begin = strtotime($gap[0]);
			$end = strtotime($gap[1]);
		}
		// 搜索条件
		$condition = array();
		I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
		if ($begin && $end) {
			$condition['add_time'] = array('between', "$begin,$end");
		}
		I('order_sn') ? $condition['order_sn'] = trim(I('order_sn')) : false;
		I('order_status') != '' ? $condition['order_status'] = I('order_status') : false;
		I('pay_status') != '' ? $condition['pay_status'] = I('pay_status') : false;
		I('pay_code') != '' ? $condition['pay_code'] = I('pay_code') : false;
		I('shipping_status') != '' ? $condition['shipping_status'] = I('shipping_status') : false;
		I('user_id') ? $condition['user_id'] = trim(I('user_id')) : false;

		// 分配工序
		I('allot_status') != '' ? $condition['allot_status'] = I('allot_status') : false;

		// 查看角色id 如果是超级管理员能操作分配订单
		$admin_info = getAdminInfo(session('admin_id'));

		$sort_order = I('order_by', 'DESC') . ' ' . I('sort');
		$count = M('order') -> where($condition) -> count();
		$Page = new AjaxPage($count, 10000);
		//  搜索条件下 分页赋值
		foreach ($condition as $key => $val) {
			$Page -> parameter[$key] = urlencode($val);
		}
		$show = $Page -> show();
		

		//刷新订单时候更改 订单状态 方法和用户者页面一样
		$results = D('order');

		$orderList2 = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		$small = D('small_order');
		foreach ($orderList2 as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select * from ty_small_order where order_sn = '{$order_sn}'";
			$smallarr = $small -> query($sql);
			$i = 0;
			foreach ($smallarr as $k => $v3) {//already_offer
				if ($v3['allot_status'] == 2 && $v3['audit'] == 1) {
					$i++;
				}
			}

			if ($i == count($smallarr) && $i != 0 && $v['already_offer'] != 2 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 1);
				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
			if ($v['already_offer'] != 4) {
				foreach ($smallarr as $k => $v2) {//already_offer
					if ($v2['jiaqian'] != 0) {
						$arr = array('already_offer' => 2);
						$results -> where("order_sn = '{$order_sn}'") -> save($arr);
					}
				}
			}
			$qian = $results -> where("order_sn = '{$order_sn}'") -> field('total_amount') -> select();

			if ($qian[0]['total_amount'] != 0 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 3);

				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
		}
		//获取订单列表
		$orderList = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		//		$orderList = $small -> query($sql);\
	
		$order = D('small_order');
		$fpgx = D('fpgx');
		$gx = D('gx');
		$processing_state =D("processing_state");
		$weiqi_id = $admin_info['admin_id'];
		foreach ($orderList as $key => $v) {
			//判断加工情况 
			 $sn = $v['order_sn'];
			$psarr = $processing_state ->where("order_sn = '{$sn}' and inspection = 0 ") ->find();
			if($psarr){
				$orderList[$key]['psarr']= 0;//加工未完成 
			}else{
				$orderList[$key]['psarr']= 1;//加工完成待发货
			}

			
			$sql = "select * from ty_small_order , ty_users where ty_small_order.user_id = ty_users.user_id and order_sn = '{$sn}'";
			$orderList1 = $small -> query($sql);
			foreach ($orderList1 as $key2 => $v) {

				//查询判断订单工序的报价情况，如果全部都有报价则返回2保存到订单allot_statut 里面 2对应微企已报价
				$fpgxsql = "select id,shuliang from ty_fpgx where dingdan = {$v["order_sn"]} and tuhao ='{$v['chart_number']}'";
				$fpgx_arr = $fpgx -> query($fpgxsql);
				$gxsql = "select count(status) from ty_gx where fpgxid = '{$fpgx_arr[0]['id']}'";
				if ($v['allot_status'] == 1) {//先判断是否订单是否已经分配工序
					$gxsql = $gx -> query($gxsql);
					if ($fpgx_arr[0]["shuliang"] == $gxsql[0]['count(status)']) {
						$data['allot_status'] = 2;
						$order -> where("order_sn={$v["order_sn"]} and chart_number ='{$v['chart_number']}' ") -> save($data);
						// 根据条件更新记录
					}
					//    上面实现判断工序报价状况下面实现判断微企对这个订单有没有报价已经报价为1否则为0
				}
				$orderList[$key]["wq_id"] = 0;
				$source = $v['weiqi_id'];

				if($source == null){
					$orderList[$key]["qq"] = 1;//判断订单是否已经报价
				}else{
					$orderList[$key]["qq"] = 0;//已经报价
				}
				$sourcearr = explode(',', $source);
				//var_dump($sourcearr);
				foreach ($sourcearr as $v) {
					if ($v == $weiqi_id) {

						$orderList[$key]["wq_id"] = 1;
						
					}

				}

			}
		}
		
		// 20180 1 11 判断订单已选择图号里面是否存在当前登录的微企

		$weiqi_name = $admin_info['company_name'];
		$chart_price = D('chart_price');

		foreach ($orderList as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select id  from ty_chart_price where  order_sn = '{$order_sn}' and qiyename = '{$weiqi_name}' and judge_choose = 1 ";
			$arr = $chart_price -> query($sql);

			foreach ($arr as $arrv) {
				if ($arrv['id'] != null) {

					$orderList[$k]['bb'] = 1;
				} else {
					$orderList[$k]['bb'] = 0;
				}

			}
		}
		
		$i = 0;

		foreach ( $orderList as $key => $value) {
			
			if($value["wq_id"] == 1){
				$arrs[$i]=  $value;
				$i++;
			}
		}
		
		
		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
		$i = 0;
		if($admin_info['admin_id'] == 1){

		foreach ( $orderList as $key => $value) {
		//var_dump($value['allot_status']);
			if($value["qq"] == 0 ){
				$arr[$i]=  $value;
				$i++;
			}
		}

		$this -> assign('orderList', $arr);
}else{
		$this -> assign('orderList', $arrs);
}
		// 赋值分页输出
		$this -> display();
	}

	public function selectindex(){
		$begin = date('Y/m/d', (time() - 30 * 60 * 60 * 24));
		//30天前
		$end = date('Y/m/d', strtotime('+1 days'));
		$this -> assign('timegap', $begin . '-' . $end);
		$this -> display();
	}
	public function selectajax(){
		$orderLogic = new OrderLogic();
		$timegap = I('timegap');
		if ($timegap) {
			$gap = explode('-', $timegap);
			$begin = strtotime($gap[0]);
			$end = strtotime($gap[1]);
		}
		// 搜索条件
		$condition = array();
		I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
		if ($begin && $end) {
			$condition['add_time'] = array('between', "$begin,$end");
		}
		I('order_sn') ? $condition['order_sn'] = trim(I('order_sn')) : false;
		I('order_status') != '' ? $condition['order_status'] = I('order_status') : false;
		I('pay_status') != '' ? $condition['pay_status'] = I('pay_status') : false;
		I('pay_code') != '' ? $condition['pay_code'] = I('pay_code') : false;
		I('shipping_status') != '' ? $condition['shipping_status'] = I('shipping_status') : false;
		I('user_id') ? $condition['user_id'] = trim(I('user_id')) : false;

		// 分配工序
		I('allot_status') != '' ? $condition['allot_status'] = I('allot_status') : false;

		// 查看角色id 如果是超级管理员能操作分配订单
		$admin_info = getAdminInfo(session('admin_id'));

		$sort_order = I('order_by', 'DESC') . ' ' . I('sort');
		$count = M('order') -> where($condition) -> count();
		$Page = new AjaxPage($count, 10000);
		//  搜索条件下 分页赋值
		foreach ($condition as $key => $val) {
			$Page -> parameter[$key] = urlencode($val);
		}
		$show = $Page -> show();
		

		//刷新订单时候更改 订单状态 方法和用户者页面一样
		$results = D('order');

		$orderList2 = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		$small = D('small_order');
		foreach ($orderList2 as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select * from ty_small_order where order_sn = '{$order_sn}'";
			$smallarr = $small -> query($sql);
			$i = 0;
			foreach ($smallarr as $k => $v3) {//already_offer
				if ($v3['allot_status'] == 2 && $v3['audit'] == 1) {
					$i++;
				}
			}

			if ($i == count($smallarr) && $i != 0 && $v['already_offer'] != 2 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 1);
				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
			if ($v['already_offer'] != 4) {
				foreach ($smallarr as $k => $v2) {//already_offer
					if ($v2['jiaqian'] != 0) {
						$arr = array('already_offer' => 2);
						$results -> where("order_sn = '{$order_sn}'") -> save($arr);
					}
				}
			}
			$qian = $results -> where("order_sn = '{$order_sn}'") -> field('total_amount') -> select();

			if ($qian[0]['total_amount'] != 0 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 3);

				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
		}
		//获取订单列表
		$orderList = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		//		$orderList = $small -> query($sql);\
	
		$order = D('small_order');
		$fpgx = D('fpgx');
		$gx = D('gx');
		$processing_state =D("processing_state");
		$weiqi_id = $admin_info['admin_id'];
		foreach ($orderList as $key => $v) {
			//判断加工情况 
			 $sn = $v['order_sn'];
			$psarr = $processing_state ->where("order_sn = '{$sn}' and inspection = 0 ") ->find();
			if($psarr){
				$orderList[$key]['psarr']= 0;//加工未完成 
			}else{
				$orderList[$key]['psarr']= 1;//加工完成待发货
			}

			
			$sql = "select * from ty_small_order , ty_users where ty_small_order.user_id = ty_users.user_id and order_sn = '{$sn}'";
			$orderList1 = $small -> query($sql);
			foreach ($orderList1 as $key2 => $v) {

				//查询判断订单工序的报价情况，如果全部都有报价则返回2保存到订单allot_statut 里面 2对应微企已报价
				$fpgxsql = "select id,shuliang from ty_fpgx where dingdan = {$v["order_sn"]} and tuhao ='{$v['chart_number']}'";
				$fpgx_arr = $fpgx -> query($fpgxsql);
				$gxsql = "select count(status) from ty_gx where fpgxid = '{$fpgx_arr[0]['id']}'";
				if ($v['allot_status'] == 1) {//先判断是否订单是否已经分配工序
					$gxsql = $gx -> query($gxsql);
					if ($fpgx_arr[0]["shuliang"] == $gxsql[0]['count(status)']) {
						$data['allot_status'] = 2;
						$order -> where("order_sn={$v["order_sn"]} and chart_number ='{$v['chart_number']}' ") -> save($data);
						// 根据条件更新记录
					}
					//    上面实现判断工序报价状况下面实现判断微企对这个订单有没有报价已经报价为1否则为0
				}
				$orderList[$key]["wq_id"] = 0;
				$source = $v['weiqi_id'];
				$sourcearr = explode(',', $source);
				foreach ($sourcearr as $v) {
					if ($v == $weiqi_id) {

						$orderList[$key]["wq_id"] = 1;
						
					}

				}

			}
		}
		
		// 20180 1 11 判断订单已选择图号里面是否存在当前登录的微企

		$weiqi_name = $admin_info['company_name'];
		$chart_price = D('chart_price');

		foreach ($orderList as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select id  from ty_chart_price where  order_sn = '{$order_sn}' and qiyename = '{$weiqi_name}' and judge_choose = 1 ";
			$arr = $chart_price -> query($sql);

			foreach ($arr as $arrv) {
				if ($arrv['id'] != null) {

					$orderList[$k]['bb'] = 1;
				} else {
					$orderList[$k]['bb'] = 0;
				}

			}
			$sql = "select id  from ty_chart_price where  order_sn = '{$order_sn}'  and judge_choose = 1 ";
			$arr = $chart_price -> query($sql);//用来区分订单 有没有被选择用于admin查看被选择的单
			if($arr == null){
				$orderList[$k]['cc'] = 1;//这个订单图号还没有被选择上。
			}else{
				$orderList[$k]['cc'] = 0;//订单已经有客户选择了。
			}
		}
		
		$i = 0;

		foreach ( $orderList as $key => $value) {
			
			if($value["bb"] == 1){
				$arrs[$i]=  $value;
				$i++;
			}
		}
		
		
		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
		$i=0;
		if($admin_info['admin_id'] == 1){
			foreach ( $orderList as $key => $value) {
			
			if($value["cc"] == 0){
				$arr[$i]=  $value;
				$i++;
			}
		}
		$this -> assign('orderList', $arr);
}else{
	
		$this -> assign('orderList', $arrs);
}
		// 赋值分页输出
		$this -> display();
	}
	// public function selectindex(){

	// }
	// public function selectajax(){
		
	// }
	// 	public function selectindex(){

	// }
	// public function selectajax(){
		
	// }

	/*
	 *已收款
	 */
	public function phbrList() {//已收款
		$begin = date('Y/m/d', (time() - 30 * 60 * 60 * 24));
		//30天前
		$end = date('Y/m/d', strtotime('+1 days'));
		$this -> assign('timegap', $begin . '-' . $end);
		$this -> display();
	}
	/*
	 *未收款
	 */
	public function uncollectedList() {//未收款
		$begin = date('Y/m/d', (time() - 30 * 60 * 60 * 24));
		//30天前
		$end = date('Y/m/d', strtotime('+1 days'));
		$this -> assign('timegap', $begin . '-' . $end);
		$this -> display();
	}
	/*
	 *线下支付
	 */
	public function offlinePaymentList() {//线下支付
		$begin = date('Y/m/d', (time() - 30 * 60 * 60 * 24));
		//30天前
		$end = date('Y/m/d', strtotime('+1 days'));
		$this -> assign('timegap', $begin . '-' . $end);
		$this -> display();
	}
	public function ajaxphbrlist(){

		$orderLogic = new OrderLogic();
		$timegap = I('timegap');
		if ($timegap) {
			$gap = explode('-', $timegap);
			$begin = strtotime($gap[0]);
			$end = strtotime($gap[1]);
		}
		// 搜索条件
		$condition = array();
		I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
		if ($begin && $end) {
			$condition['add_time'] = array('between', "$begin,$end");
		}
		I('order_sn') ? $condition['order_sn'] = trim(I('order_sn')) : false;
		I('order_status') != '' ? $condition['order_status'] = I('order_status') : false;
		I('pay_status') != '' ? $condition['pay_status'] = I('pay_status') : false;
		I('pay_code') != '' ? $condition['pay_code'] = I('pay_code') : false;
		I('shipping_status') != '' ? $condition['shipping_status'] = I('shipping_status') : false;
		I('user_id') ? $condition['user_id'] = trim(I('user_id')) : false;

		// 分配工序
		I('allot_status') != '' ? $condition['allot_status'] = I('allot_status') : false;

		// 查看角色id 如果是超级管理员能操作分配订单
		$admin_info = getAdminInfo(session('admin_id'));

		$sort_order = I('order_by', 'DESC') . ' ' . I('sort');
		$count = M('order') -> where($condition) -> count();
		$Page = new AjaxPage($count, 10000);
		//  搜索条件下 分页赋值
		foreach ($condition as $key => $val) {
			$Page -> parameter[$key] = urlencode($val);
		}
		$show = $Page -> show();
		

		//刷新订单时候更改 订单状态 方法和用户者页面一样
		$results = D('order');

		$orderList2 = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		$small = D('small_order');
		foreach ($orderList2 as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select * from ty_small_order where order_sn = '{$order_sn}'";
			$smallarr = $small -> query($sql);
			$i = 0;
			foreach ($smallarr as $k => $v3) {//already_offer
				if ($v3['allot_status'] == 2 && $v3['audit'] == 1) {
					$i++;
				}
			}

			if ($i == count($smallarr) && $i != 0 && $v['already_offer'] != 2 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 1);
				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
			if ($v['already_offer'] != 4) {
				foreach ($smallarr as $k => $v2) {//already_offer
					if ($v2['jiaqian'] != 0) {
						$arr = array('already_offer' => 2);
						$results -> where("order_sn = '{$order_sn}'") -> save($arr);
					}
				}
			}
			$qian = $results -> where("order_sn = '{$order_sn}'") -> field('total_amount') -> select();

			if ($qian[0]['total_amount'] != 0 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 3);

				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
		}
		//获取订单列表
		$orderList = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		//		$orderList = $small -> query($sql);\
	
		$order = D('small_order');
		$fpgx = D('fpgx');
		$gx = D('gx');
		$processing_state =D("processing_state");
		$weiqi_id = $admin_info['admin_id'];
		foreach ($orderList as $key => $v) {
			//判断加工情况 
			 $sn = $v['order_sn'];
			$psarr = $processing_state ->where("order_sn = '{$sn}' and inspection = 0 ") ->find();
			if($psarr){
				$orderList[$key]['psarr']= 0;//加工未完成 
			}else{
				$orderList[$key]['psarr']= 1;//加工完成待发货
			}

			
			$sql = "select * from ty_small_order , ty_users where ty_small_order.user_id = ty_users.user_id and order_sn = '{$sn}'";
			$orderList1 = $small -> query($sql);
			foreach ($orderList1 as $key => $v) {

				//查询判断订单工序的报价情况，如果全部都有报价则返回2保存到订单allot_statut 里面 2对应微企已报价
				$fpgxsql = "select id,shuliang from ty_fpgx where dingdan = {$v["order_sn"]} and tuhao ='{$v['chart_number']}'";
				$fpgx_arr = $fpgx -> query($fpgxsql);
				$gxsql = "select count(status) from ty_gx where fpgxid = '{$fpgx_arr[0]['id']}'";
				if ($v['allot_status'] == 1) {//先判断是否订单是否已经分配工序
					$gxsql = $gx -> query($gxsql);
					if ($fpgx_arr[0]["shuliang"] == $gxsql[0]['count(status)']) {
						$data['allot_status'] = 2;
						$order -> where("order_sn={$v["order_sn"]} and chart_number ='{$v['chart_number']}' ") -> save($data);
						// 根据条件更新记录
					}
					//    上面实现判断工序报价状况下面实现判断微企对这个订单有没有报价已经报价为1否则为0
				}
				$orderList[$key]["wq_id"] = 0;
				$source = $v['weiqi_id'];
				$sourcearr = explode(',', $source);
				foreach ($sourcearr as $v) {
					if ($v == $weiqi_id) {
						$orderList[$key]["wq_id"] = 1;
					}
				}

			}
		}
		// 20180 1 11 判断订单已选择图号里面是否存在当前登录的微企

		$weiqi_name = $admin_info['company_name'];
		$chart_price = D('chart_price');

		foreach ($orderList as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select id  from ty_chart_price where  order_sn = '{$order_sn}' and qiyename = '{$weiqi_name}' and judge_choose = 1 ";
			$arr = $chart_price -> query($sql);

			foreach ($arr as $arrv) {
				if ($arrv['id'] != null) {

					$orderList[$k]['bb'] = 1;
				} else {
					$orderList[$k]['bb'] = 0;
				}

			}
		}
		//		foreach ($orderList as $k => $v) {
		//			$order_sn = $v['order_sn'];
		//
		//			if ($v['bb'] == 1) {
		//				$smalllist = $small -> where("order_sn = '{$order_sn}' ") -> select();
		//
		//				foreach ($smalllist as $k2 => $v2) {
		//
		//					if ($v2['judge_choose'] = 1) {
		//						$ok = 1;
		//
		//					}
		//				}
		//				if ($ok != 1) {
		//					$orderList[$k]['bb'] = 0;
		//				}
		//			}

		//		}

		$i = 0;

		foreach ( $orderList as $key => $value) {
			
			if($value["bb"] == 1 and $value["pay_s_jj"] > 0){
				$arrs[$i]=  $value;
				$i++;
			}
		}
		$i = 0;
		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
		if($admin_info['admin_id'] == 1){

		foreach ( $orderList as $key => $value) {
		//var_dump($value['allot_status']);
			if($value["pay_s_jj"] > 0 ){
				$arr[$i]=  $value;
				$i++;
			}
		}

		$this -> assign('orderList', $arr);
}else{
		$this -> assign('orderList', $arrs);
}
		// 赋值分页输出
		$this -> display();
	}
	public function ajaxuncollectedlist(){
		
		$orderLogic = new OrderLogic();
		$timegap = I('timegap');
		if ($timegap) {
			$gap = explode('-', $timegap);
			$begin = strtotime($gap[0]);
			$end = strtotime($gap[1]);
		}
		// 搜索条件
		$condition = array();
		I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
		if ($begin && $end) {
			$condition['add_time'] = array('between', "$begin,$end");
		}
		I('order_sn') ? $condition['order_sn'] = trim(I('order_sn')) : false;
		I('order_status') != '' ? $condition['order_status'] = I('order_status') : false;
		I('pay_status') != '' ? $condition['pay_status'] = I('pay_status') : false;
		I('pay_code') != '' ? $condition['pay_code'] = I('pay_code') : false;
		I('shipping_status') != '' ? $condition['shipping_status'] = I('shipping_status') : false;
		I('user_id') ? $condition['user_id'] = trim(I('user_id')) : false;

		// 分配工序
		I('allot_status') != '' ? $condition['allot_status'] = I('allot_status') : false;

		// 查看角色id 如果是超级管理员能操作分配订单
		$admin_info = getAdminInfo(session('admin_id'));

		$sort_order = I('order_by', 'DESC') . ' ' . I('sort');
		$count = M('order') -> where($condition) -> count();
		$Page = new AjaxPage($count, 10000);
		//  搜索条件下 分页赋值
		foreach ($condition as $key => $val) {
			$Page -> parameter[$key] = urlencode($val);
		}
		$show = $Page -> show();
		

		//刷新订单时候更改 订单状态 方法和用户者页面一样
		$results = D('order');

		$orderList2 = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		$small = D('small_order');
		foreach ($orderList2 as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select * from ty_small_order where order_sn = '{$order_sn}'";
			$smallarr = $small -> query($sql);
			$i = 0;
			foreach ($smallarr as $k => $v3) {//already_offer
				if ($v3['allot_status'] == 2 && $v3['audit'] == 1) {
					$i++;
				}
			}

			if ($i == count($smallarr) && $i != 0 && $v['already_offer'] != 2 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 1);
				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
			if ($v['already_offer'] != 4) {
				foreach ($smallarr as $k => $v2) {//already_offer
					if ($v2['jiaqian'] != 0) {
						$arr = array('already_offer' => 2);
						$results -> where("order_sn = '{$order_sn}'") -> save($arr);
					}
				}
			}
			$qian = $results -> where("order_sn = '{$order_sn}'") -> field('total_amount') -> select();

			if ($qian[0]['total_amount'] != 0 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 3);

				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
		}
		//获取订单列表
		$orderList = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		//		$orderList = $small -> query($sql);\
	
		$order = D('small_order');
		$fpgx = D('fpgx');
		$gx = D('gx');
		$processing_state =D("processing_state");
		$weiqi_id = $admin_info['admin_id'];
		foreach ($orderList as $key => $v) {
			//判断加工情况 
			 $sn = $v['order_sn'];
			$psarr = $processing_state ->where("order_sn = '{$sn}' and inspection = 0 ") ->find();
			if($psarr){
				$orderList[$key]['psarr']= 0;//加工未完成 
			}else{
				$orderList[$key]['psarr']= 1;//加工完成待发货
			}

			
			$sql = "select * from ty_small_order , ty_users where ty_small_order.user_id = ty_users.user_id and order_sn = '{$sn}'";
			$orderList1 = $small -> query($sql);
			foreach ($orderList1 as $key => $v) {

				//查询判断订单工序的报价情况，如果全部都有报价则返回2保存到订单allot_statut 里面 2对应微企已报价
				$fpgxsql = "select id,shuliang from ty_fpgx where dingdan = {$v["order_sn"]} and tuhao ='{$v['chart_number']}'";
				$fpgx_arr = $fpgx -> query($fpgxsql);
				$gxsql = "select count(status) from ty_gx where fpgxid = '{$fpgx_arr[0]['id']}'";
				if ($v['allot_status'] == 1) {//先判断是否订单是否已经分配工序
					$gxsql = $gx -> query($gxsql);
					if ($fpgx_arr[0]["shuliang"] == $gxsql[0]['count(status)']) {
						$data['allot_status'] = 2;
						$order -> where("order_sn={$v["order_sn"]} and chart_number ='{$v['chart_number']}' ") -> save($data);
						// 根据条件更新记录
					}
					//    上面实现判断工序报价状况下面实现判断微企对这个订单有没有报价已经报价为1否则为0
				}
				$orderList[$key]["wq_id"] = 0;
				$source = $v['weiqi_id'];
				$sourcearr = explode(',', $source);
				foreach ($sourcearr as $v) {
					if ($v == $weiqi_id) {
						$orderList[$key]["wq_id"] = 1;
					}
				}

			}
		}
		// 20180 1 11 判断订单已选择图号里面是否存在当前登录的微企

		$weiqi_name = $admin_info['company_name'];
		$chart_price = D('chart_price');

		foreach ($orderList as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select id  from ty_chart_price where  order_sn = '{$order_sn}' and qiyename = '{$weiqi_name}' and judge_choose = 1 ";
			$arr = $chart_price -> query($sql);

			foreach ($arr as $arrv) {
				if ($arrv['id'] != null) {

					$orderList[$k]['bb'] = 1;
				} else {
					$orderList[$k]['bb'] = 0;
				}

			}
		}
		//		foreach ($orderList as $k => $v) {
		//			$order_sn = $v['order_sn'];
		//
		//			if ($v['bb'] == 1) {
		//				$smalllist = $small -> where("order_sn = '{$order_sn}' ") -> select();
		//
		//				foreach ($smalllist as $k2 => $v2) {
		//
		//					if ($v2['judge_choose'] = 1) {
		//						$ok = 1;
		//
		//					}
		//				}
		//				if ($ok != 1) {
		//					$orderList[$k]['bb'] = 0;
		//				}
		//			}
		//		}

		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
		$i = 0;

		foreach ( $orderList as $key => $value) {
			
			if($value["bb"] == 1 and $value["already_offer"] == 4 and $value["pay_s_jj"] == 0){
				$arrs[$i]=  $value;
				$i++;
			}
		}
		$i = 0;
		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
		if($admin_info['admin_id'] == 1){

		foreach ( $orderList as $key => $value) {
		//var_dump($value['allot_status']);
			if($value["already_offer"] ==4 and $value["pay_s_jj"] == 0){
				$arr[$i]=  $value;
				$i++;
			}
		}

		$this -> assign('orderList', $arr);
}else{
		$this -> assign('orderList', $arrs);
}
		
		// 赋值分页输出
		$this -> display();
	}
	public function ajaxofflinepaymentlist(){
		
		$orderLogic = new OrderLogic();
		$timegap = I('timegap');
		if ($timegap) {
			$gap = explode('-', $timegap);
			$begin = strtotime($gap[0]);
			$end = strtotime($gap[1]);
		}
		// 搜索条件
		$condition = array();
		I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
		if ($begin && $end) {
			$condition['add_time'] = array('between', "$begin,$end");
		}
		I('order_sn') ? $condition['order_sn'] = trim(I('order_sn')) : false;
		I('order_status') != '' ? $condition['order_status'] = I('order_status') : false;
		I('pay_status') != '' ? $condition['pay_status'] = I('pay_status') : false;
		I('pay_code') != '' ? $condition['pay_code'] = I('pay_code') : false;
		I('shipping_status') != '' ? $condition['shipping_status'] = I('shipping_status') : false;
		I('user_id') ? $condition['user_id'] = trim(I('user_id')) : false;

		// 分配工序
		I('allot_status') != '' ? $condition['allot_status'] = I('allot_status') : false;

		// 查看角色id 如果是超级管理员能操作分配订单
		$admin_info = getAdminInfo(session('admin_id'));

		$sort_order = I('order_by', 'DESC') . ' ' . I('sort');
		$count = M('order') -> where($condition) -> count();
		$Page = new AjaxPage($count, 10000);
		//  搜索条件下 分页赋值
		foreach ($condition as $key => $val) {
			$Page -> parameter[$key] = urlencode($val);
		}
		$show = $Page -> show();
		

		//刷新订单时候更改 订单状态 方法和用户者页面一样
		$results = D('order');

		$orderList2 = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		$small = D('small_order');
		foreach ($orderList2 as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select * from ty_small_order where order_sn = '{$order_sn}'";
			$smallarr = $small -> query($sql);
			$i = 0;
			foreach ($smallarr as $k => $v3) {//already_offer
				if ($v3['allot_status'] == 2 && $v3['audit'] == 1) {
					$i++;
				}
			}

			if ($i == count($smallarr) && $i != 0 && $v['already_offer'] != 2 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 1);
				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
			if ($v['already_offer'] != 4) {
				foreach ($smallarr as $k => $v2) {//already_offer
					if ($v2['jiaqian'] != 0) {
						$arr = array('already_offer' => 2);
						$results -> where("order_sn = '{$order_sn}'") -> save($arr);
					}
				}
			}
			$qian = $results -> where("order_sn = '{$order_sn}'") -> field('total_amount') -> select();

			if ($qian[0]['total_amount'] != 0 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 3);

				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
		}
		//获取订单列表
		$orderList = $orderLogic -> getOrderList($condition, $sort_order, $Page -> firstRow, $Page -> listRows);
		//查询order的微企id字符串判断是否存在订单里面  存在保存结果1 否则保存0；
		//		$orderList = $small -> query($sql);\
	
		$order = D('small_order');
		$fpgx = D('fpgx');
		$gx = D('gx');
		$processing_state =D("processing_state");
		$weiqi_id = $admin_info['admin_id'];
		foreach ($orderList as $key => $v) {
			//判断加工情况 
			 $sn = $v['order_sn'];
			$psarr = $processing_state ->where("order_sn = '{$sn}' and inspection = 0 ") ->find();
			if($psarr){
				$orderList[$key]['psarr']= 0;//加工未完成 
			}else{
				$orderList[$key]['psarr']= 1;//加工完成待发货
			}

			
			$sql = "select * from ty_small_order , ty_users where ty_small_order.user_id = ty_users.user_id and order_sn = '{$sn}'";
			$orderList1 = $small -> query($sql);
			foreach ($orderList1 as $key => $v) {

				//查询判断订单工序的报价情况，如果全部都有报价则返回2保存到订单allot_statut 里面 2对应微企已报价
				$fpgxsql = "select id,shuliang from ty_fpgx where dingdan = {$v["order_sn"]} and tuhao ='{$v['chart_number']}'";
				$fpgx_arr = $fpgx -> query($fpgxsql);
				$gxsql = "select count(status) from ty_gx where fpgxid = '{$fpgx_arr[0]['id']}'";
				if ($v['allot_status'] == 1) {//先判断是否订单是否已经分配工序
					$gxsql = $gx -> query($gxsql);
					if ($fpgx_arr[0]["shuliang"] == $gxsql[0]['count(status)']) {
						$data['allot_status'] = 2;
						$order -> where("order_sn={$v["order_sn"]} and chart_number ='{$v['chart_number']}' ") -> save($data);
						// 根据条件更新记录
					}
					//    上面实现判断工序报价状况下面实现判断微企对这个订单有没有报价已经报价为1否则为0
				}
				$orderList[$key]["wq_id"] = 0;
				$source = $v['weiqi_id'];
				$sourcearr = explode(',', $source);
				foreach ($sourcearr as $v) {
					if ($v == $weiqi_id) {
						$orderList[$key]["wq_id"] = 1;
					}
				}

			}
		}
		// 20180 1 11 判断订单已选择图号里面是否存在当前登录的微企

		$weiqi_name = $admin_info['company_name'];
		$chart_price = D('chart_price');

		foreach ($orderList as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select id  from ty_chart_price where  order_sn = '{$order_sn}' and qiyename = '{$weiqi_name}' and judge_choose = 1 ";
			$arr = $chart_price -> query($sql);

			foreach ($arr as $arrv) {
				if ($arrv['id'] != null) {

					$orderList[$k]['bb'] = 1;
				} else {
					$orderList[$k]['bb'] = 0;
				}

			}
		}
		//		foreach ($orderList as $k => $v) {
		//			$order_sn = $v['order_sn'];
		//
		//			if ($v['bb'] == 1) {
		//				$smalllist = $small -> where("order_sn = '{$order_sn}' ") -> select();
		//
		//				foreach ($smalllist as $k2 => $v2) {
		//
		//					if ($v2['judge_choose'] = 1) {
		//						$ok = 1;
		//
		//					}
		//				}
		//				if ($ok != 1) {
		//					$orderList[$k]['bb'] = 0;
		//				}
		//			}
		//		}

		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
			$i = 0;

		foreach ( $orderList as $key => $value) {
			
			if($value["bb"] == 1 and $value["pay_s_jj"]== 4 ){
				$arrs[$i]=  $value;
				$i++;
			}
		}
		$i = 0;
		$this -> assign('admin_info', $admin_info);
		$this -> assign('page', $show);
		if($admin_info['admin_id'] == 1){

		foreach ( $orderList as $key => $value) {
		//var_dump($value['allot_status']);
			if($value["pay_s_jj"] == 4 ){
				$arr[$i]=  $value;
				$i++;
			}
		}

		$this -> assign('orderList', $arr);
}else{
		$this -> assign('orderList', $arrs);
}
		// 赋值分页输出
		$this -> display();
	}
}